package com.hdjk.view.callback;

import com.hdjk.view.entity.BuyPlanEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class BuyPlanListCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg=super.parseNetworkResponse(response, i);
        JSONObject object= (JSONObject) msg.getData();
        List<BuyPlanEntity> list = new ArrayList<>();
        if (object!=null) {
            if (!object.isNull("data")) {
                JSONArray array = object.getJSONArray("data");
                if (array != null && array.length() > 0) {
                    for (int j = 0; j < array.length(); j++) {
                        JSONObject jsonObject = array.getJSONObject(j);
                        BuyPlanEntity entity = new BuyPlanEntity();
                        if (!jsonObject.isNull("accountNumber")) {
                            entity.setAccountNumber(jsonObject.getString("accountNumber"));
                        }
                        if (!jsonObject.isNull("rspId")) {
                            entity.setRspId(jsonObject.getInt("rspId"));
                        }
                        if (!jsonObject.isNull("portfolioName")) {
                            entity.setPortfolioName(jsonObject.getString("portfolioName"));
                        }
                        if (!jsonObject.isNull("portfolioCode")) {
                            entity.setPortfolioCode(jsonObject.getString("portfolioCode"));
                        }
                        if (!jsonObject.isNull("investorPayId")) {
                            entity.setInvestorPayId(jsonObject.getInt("investorPayId"));
                        }
                        if (!jsonObject.isNull("investorPayBankAccountNumber")) {
                            entity.setInvestorPayBankAccountNumber(jsonObject.getString("investorPayBankAccountNumber"));
                        }
                        if (!jsonObject.isNull("investorPayBankName")) {
                            entity.setInvestorPayBankName(jsonObject.getString("investorPayBankName"));
                        }
                        if (!jsonObject.isNull("investorPayBankCode")) {
                            entity.setInvestorPayBankCode(jsonObject.getString("investorPayBankCode"));
                        }
                        if (!jsonObject.isNull("rspStatus")) {
                            entity.setRspStatus(jsonObject.getString("rspStatus"));
                        }
                        if (!jsonObject.isNull("cycle")) {
                            entity.setCycle(jsonObject.getString("cycle"));
                        }
                        if (!jsonObject.isNull("cycleDay")) {
                            entity.setCycleDay(jsonObject.getInt("cycleDay"));
                        }
                        if (!jsonObject.isNull("postponeDeduction")) {
                            entity.setPostponeDeduction(jsonObject.getString("postponeDeduction"));
                        }
                        if (!jsonObject.isNull("createdDate")) {
                            entity.setCreatedDate(jsonObject.getLong("createdDate"));
                        }
                        if (!jsonObject.isNull("updatedDate")) {
                            entity.setUpdatedDate(jsonObject.getLong("updatedDate"));
                        }
                        if (!jsonObject.isNull("nextRunDate")) {
                            entity.setNextRunDate(jsonObject.getLong("nextRunDate"));
                        }
                        if (!jsonObject.isNull("name")) {
                            entity.setName(jsonObject.getString("name"));
                        }
                        if (!jsonObject.isNull("targetAmount")) {
                            entity.setTargetAmount(jsonObject.getString("targetAmount"));
                        }
                        if (!jsonObject.isNull("type")) {
                            entity.setType(jsonObject.getString("type"));
                        }
                        if (!jsonObject.isNull("standingInstructionNum")) {
                            entity.setStandingInstructionNum(jsonObject.getString("standingInstructionNum"));
                        }
                        if (!jsonObject.isNull("products")){
                            JSONArray jsonArray=jsonObject.getJSONArray("products");
                            List<BuyPlanEntity.Products> productses=new ArrayList<>();
                            if (jsonArray!=null){
                                for (int n=0;n<jsonArray.length();n++){
                                    BuyPlanEntity.Products products=new BuyPlanEntity().new Products();
                                    JSONObject json=jsonArray.getJSONObject(n);
                                    if (!json.isNull("fundCode")) {
                                        products.setFundCode(json.getString("fundCode"));
                                    }
                                    if (!json.isNull("fundName")) {
                                        products.setFundName(json.getString("fundName"));
                                    }
                                    if (!json.isNull("rspAmount")) {
                                        products.setRspAmount(json.getString("rspAmount"));
                                    }
                                    if (!json.isNull("currency")) {
                                        products.setCurrency(json.getString("currency"));
                                    }
                                    productses.add(products);
                                }
                            }
                            entity.setProducts(productses);
                        }
                        list.add(entity);
                    }
                }
            }
        }
        msg.setData(list);
        return msg;
    }
}
